package com.jt.factory;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.beans.factory.FactoryBean;

import java.io.InputStream;

public class SessionFactory implements FactoryBean<SqlSession> {
    InputStream in;
    SqlSessionFactory factory;
    @Override
    public SqlSession getObject() throws Exception {
        in= Resources.getResourceAsStream("MybatisConfig.xml");
         factory= new SqlSessionFactoryBuilder().build(in);
        System.out.println("新建获取了session");
        return factory.openSession(true);
    }
    public void destory(){
        try {
        in.close();
           factory=null;
        }catch(Exception e){
            e.printStackTrace();
        }

    }
    @Override
    public Class<?> getObjectType() {
        return null;
    }

    @Override
    public boolean isSingleton() {
        return false;
    }


}
